package com.niubility.controller;

import com.niubility.shiro.ShiroService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;


/**
 * 版本：/shiro_3
 */
    @Controller("ShiroController")
    @RequestMapping("/shiro")
    public class ShiroController {
    @Autowired
    private ShiroService shiroService;

//    //测试方法是否调用
//    @RequestMapping("/testMethod")
//    @ResponseBody
//    public String testShiroAnnotation(HttpSession session) {
//        session.setAttribute("key", "value123");
//        shiroService.testMethod();
//        return "redirect:/list.jsp";
//    }
//
//    @RequestMapping("/login")
//    @ResponseBody
//    public String login(@RequestParam("userTel")String userTel,
//                        @RequestParam("userPwd") String userPwd) {
//        Subject currentUser = SecurityUtils.getSubject();
//        if (!currentUser.isAuthenticated()) {
//            //把用户名和密码封装为UsernamePasswordToken对象
//            UsernamePasswordToken token = new UsernamePasswordToken(userTel, userPwd);
//            //rememberme
//            token.setRememberMe(true);
//            try {
//                //执行登陆,
//                currentUser.login(token);
//                //System.out.println("登陆成功");
//            }
//            catch (AuthenticationException ae) {
//                //System.out.println("登陆失败:"+ae.getMessage());
//            }
//        }
//        return "redirect:/list.jsp";
//    }
}
